//
//  PosterCollectionView.m
//  MyMovie
//
//  Created by Weiwei on 16/2/29.
//  Copyright © 2016年 bever贝沃汇力 weiwei. All rights reserved.
//

#import "PosterCollectionView.h"
#import "HomeModel.h"
#import "PosterCollectionViewCell.h"//大海报试图的cell
@implementation PosterCollectionView

#pragma -mark子类化CollectionView
-(id)initWithFrame:(CGRect)frame{
    


#pragma -mark 改成继承baseCollectionView
//    self = [super initWithFrame:frame collectionViewLayout:layout];
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
#pragma -mark 子类自己决定单元格大小
        self.itemWidth = frame.size.width - 120;
#pragma -mark 自己自己决定
        [self registerClass:[PosterCollectionViewCell class] forCellWithReuseIdentifier:@"poster_cell"];

    }
    
    return self;
    
    
}
#pragma -mark子类复写，创建cell
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
   
#pragma -mark 重写创建cell
    
    PosterCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poster_cell" forIndexPath:indexPath];

    //获得当前的model
    //如果要想得到父类提供的@property设置的属性，用self.
    HomeModel* model = [self.dataList objectAtIndex:indexPath.item];
    cell.model = model;
    

    
    return cell;
    
}



#pragma -mark 重写父类的方法
//cell被点，然后滑动到collectionView的中心点
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    

    
    //判断当前中心页和我们点击的页是否相同，如果相同就反转
    if (indexPath.row == self.currentIndex) {
        
        //1.通过indexPath获得当前的cell
        PosterCollectionViewCell* cell  = (PosterCollectionViewCell*) [collectionView cellForItemAtIndexPath:indexPath];
        //2.调用自己的翻转方法
        [cell filpView];
        
        
    }else{
        //当点击的cell不是中间cell，不反转而是平移
         //让某一个cell滑动到相应的位置
            [collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        //平移结束，要把该cell的indexPath记录成中心页
        self.currentIndex = indexPath.row;
        
    }
    
}

#pragma -mark 为了解决Bug
//当单元格结束显示的时候
//重新将cell的页面正面设置成显示，反面隐藏，isfront == yes
-(void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
    
    //1.获得当前的cell
    PosterCollectionViewCell* poseterCell = (PosterCollectionViewCell*)cell;
    
    //2.将正面显示，反面隐藏，isfornt == yes
    poseterCell.isFront = YES;
    poseterCell.frontView.hidden = NO;
    poseterCell.reverseView.hidden = YES;
    
    
}



@end
